﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
//using System.Windows.Shapes;
using System.Windows.Navigation;
using System.IO.IsolatedStorage;

using System.IO;

using System.Collections;
using System.Text;

namespace AdvancedSilverlightLab1
{
    public partial class Page3 : Page
    {
       
        public Page3()
        {
            InitializeComponent();
            using (var IsoData = IsolatedStorageFile.GetUserStoreForApplication())
            {
                IsoData.CreateDirectory("Dir1");
                IsoData.CreateDirectory("Dir2");
                IsoData.CreateDirectory("Dir3");
                DirListbox.ItemsSource = IsoData.GetDirectoryNames();
                
                
                using (StreamWriter sw = new StreamWriter(IsoData.OpenFile("Dir1\\MyStore.Text", FileMode.OpenOrCreate, FileAccess.Write)))
                {
                    sw.WriteLine("kdksdl sadksaldmsad sadkasdmaskdm");
                }
                using (StreamWriter sw = new StreamWriter(IsoData.OpenFile("Dir2\\lsdd.Text", FileMode.OpenOrCreate, FileAccess.Write)))
                {
                    sw.WriteLine("kdksdl sadksaldmsad czxczxczxczxczxczxczxczxczxczxc cz zczxc zczcz xcz czx cz");
                }
                //IsoData.CreateFile(".");  
                quata.Text = "Available space in isolated storage: " + IsoData.UsedSize.ToString() + " / " + IsoData.Quota.ToString(); ;

            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if ((string)FileListbox.SelectedItem !=null)
            {
                using (var IsoData = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    textBoxname.Text = (string)FileListbox.SelectedItem;
                    using (StreamReader sw = new StreamReader(IsoData.OpenFile(Path.Combine((string)DirListbox.SelectedItem, (string)FileListbox.SelectedItem), FileMode.Open, FileAccess.Read)))
                    {
                        //IsoData.OpenFile((string)DirListbox.SelectedItem, System.IO.FileMode.Open);

                        textBox1.Text = sw.ReadToEnd();
                    }
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if ((string)DirListbox.SelectedItem != null)
            {
                using (var IsoData = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //if((IsoData.GetDirectoryNames(Path.Combine((string)DirListbox.SelectedItem, "*.*")))!=null)
                    //DirListbox.ItemsSource = IsoData.GetDirectoryNames(Path.Combine((string)DirListbox.SelectedItem, "*.*"));
                    FileListbox.ItemsSource = IsoData.GetFileNames(Path.Combine((string)DirListbox.SelectedItem, "*.*"));
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            using (var IsoData = IsolatedStorageFile.GetUserStoreForApplication())
            {
                DirListbox.ItemsSource = IsoData.GetDirectoryNames();
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            using (var IsoData = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (StreamWriter sw = new StreamWriter(IsoData.OpenFile(Path.Combine((string)DirListbox.SelectedItem, (string)FileListbox.SelectedItem), FileMode.Open, FileAccess.Write)))
                {
                    sw.WriteLine(textBox1.Text);
                }
            }
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            using (var IsoData = IsolatedStorageFile.GetUserStoreForApplication())
            {

                IsoData.IncreaseQuotaTo(IsoData.Quota+20);
                MessageBox.Show("Quata now is  " + IsoData.Quota.ToString());
                quata.Text = "Available space in isolated storage: " + IsoData.UsedSize.ToString() + " / " + IsoData.Quota.ToString(); ;

            }
        }

    }
}
